package io;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.util.List;

/**
 * @Author: ycy
 * @CreateDate: 2019/3/29 15:51
 */
public class FileDemo {

  public static void main(String[] args) {
    FileDemo fileDemo = new FileDemo();
    List<String> stringList = fileDemo.readFile();
    for (String s : stringList) {
      System.out.println(s);
    }
    System.out.println("读取了" + stringList.size());
  }

  /**
   * 火车线路
   *
   * @return
   */
  public List<String> readFile() {
    List<String> list = Lists.newLinkedList();
//    InputStream resourceAsStream = this.getClass().getResourceAsStream("D:\\Users\\yincy\\Desktop\\火车票线路\\火车票线路.txt");


    try {
      return FileUtils.readLines(new File("D:\\Users\\yincy\\Desktop\\火车票线路\\火车票线路.txt"), "UTF-8");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 站和城市对应关系
   *
   * @return
   */
  public List<String> readFile2() {
    List<String> list = Lists.newLinkedList();
    InputStream resourceAsStream = this.getClass().getResourceAsStream("D:\\Users\\yincy\\Desktop\\火车票线路\\城市和车站信息对应关系.txt");
    InputStreamReader isr = null;
    try {
      isr = new InputStreamReader(resourceAsStream, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        list.add(br.readLine());
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      try {
        isr.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return list;
  }


}
